// pages/book-detail/book-detail.js

const requestToGetBill = require('../../utils/request.js').requestToGetBill
const requestToDeleteBook = require('../../utils/request.js').requestToDeleteBook
const requestToGetBookDetail = require('../../utils/request.js').requestToGetBookDetail


const toBillDeatilPage = require('../../utils/wx-navigate.js').toBillDeatilPage
const toEditBookPage = require('../../utils/wx-navigate.js').toEditBookPage
const successBackPage = require('../../utils/wx-navigate.js').successBackPage


const showDialog = require('../../utils/util.js').showDialog
const billSortByMonth = require('../../utils/util.js').billSortByMonth


//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabArray: [{ prop: '', label: '租客信息' }, { prop: '', label: '账单记录' }],
    book: {},
    billList: [],
    currentIndex: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let data = JSON.parse(options.data)
    this.setData({
      book:data
    })
    console.log('----')
    this.toGetData()
    app.updateBookDeatilPageData = this.toGetData
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.toGetData()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  payTabClick(e) {
    this.setData({
      currentIndex: e.detail.currentIndex
    })

  },

  toGetData() {
   
    requestToGetBookDetail(this.data.book.book._id,res=>{
      let book = this.data.book
      book.book.rent_month_count = res.book.rent_month_count
      book.book.money_per_month = res.book.money_per_month
      book.book.rent_end_date = res.book.rent_end_date
      this.setData({
        book: book
      })

    })

    requestToGetBill(this.data.book.book._id, "SUCCESS&&NOTPAY&&CLOSED", res => {

      let billList =  res.list
      for (let i = 0; i < billList.length;i++){
        let _bill = billList[i]
        if (_bill.time_end) {
          _bill.time_end = _bill.time_end.substr(0, 4) + "/" + _bill.time_end.substr(4, 2) + "/" + _bill.time_end.substr(6, 2) + " " + _bill.time_end.substr(8, 2) + ":" + _bill.time_end.substr(10, 2) + ":" + _bill.time_end.substr(12, 2)
        }

        billList[i] = _bill
      }

      

      this.setData({
        billList: billList
      })
      
    })



  },

  bindbillitemtap: function (e) {
    toBillDeatilPage(e.currentTarget.dataset.bill)
  },

  bindeditbooktap:function(){

    toEditBookPage(this.data.book)
  },
  binddeletebooktap:function () {
    console.log(this.data.book.book._id)
    showDialog("租客退房", "确认租客退房之后将删除该租客的租房信息，请和租客确认好已结清所有房租", () => {
      requestToDeleteBook(this.data.book.book._id,()=>{
        // console.log('退房成功')
        app.updateIndexPageData()
        successBackPage('退房成功')
        
      })
    })
  },

  bindphonetap(){
    wx.makePhoneCall({
      phoneNumber: this.data.book.book.renter_phone //仅为示例，并非真实的电话号码
    })
  }
  
})